package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author gsy
 * @date 2021年08月24日18:27
 * Description
 */
@RestController
@RequestMapping("/admin/product")
public class ManageController {

    @Autowired
    private ManageService manageService;

    //查询一级分类
    @GetMapping("/getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> baseCategory1s = manageService.getCategory1();
        return Result.ok(baseCategory1s);
    }

    //查询二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2s = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2s);
    }

    //查询三级分类
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        List<BaseCategory3> baseCategory3s = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3s);
    }

    //查询平台属性
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id) {
        List<BaseAttrInfo> baseAttrInfos = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfos);
    }


    //添加或修改平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }


    //根据平台属性id查询对应的属性及其属性值
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId) {
        List<BaseAttrValue> baseAttrValues = manageService.getAttrValueList(attrId);
        return Result.ok(baseAttrValues);

    }

    /**
     * @param page  第几页
     * @param limit 每页数量
     * @return
     */
    //获取品牌分页列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit) {
        IPage<BaseTrademark> iPage = manageService.baseTrademark(page, limit);
        return Result.ok(iPage);

    }

    //根据删除品牌
    @DeleteMapping("/baseTrademark/remove/{trademarkId}")
    public Result remove(@PathVariable Long trademarkId) {
        manageService.remove(trademarkId);
        return Result.ok();

    }

    //添加品牌
    @PostMapping("/baseTrademark/save")
    public Result saveTrademark(@RequestBody BaseTrademark baseTrademark) {
        manageService.saveTrademark(baseTrademark);
        return Result.ok();

    }

    //根据品牌id查询品牌信息
    @GetMapping("/baseTrademark/get/{trademarkId}")
    public Result getTrademark(@PathVariable Long trademarkId) {
        BaseTrademark baseTrademark = manageService.getTrademark(trademarkId);
        return Result.ok(baseTrademark);
    }

    //修改品牌信息
    @PutMapping("//baseTrademark/update")
    public Result update(@RequestBody BaseTrademark baseTrademark) {
        manageService.updateTrademark(baseTrademark);
        return Result.ok();

    }

    @ApiOperation("获取spu分页列表")
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoList(
            @PathVariable @ApiParam(value = "第几页", required = true) Integer page,
            @PathVariable @ApiParam(value = "每页记录数", required = true) Integer limit,
            @ApiParam(value = "三级分类id", required = true) Long category3Id) {
        IPage<SpuInfo> iPage = manageService.getSpuInfoList(page, limit, category3Id);
        return Result.ok(iPage);

    }

    @ApiOperation("获取品牌属性列表")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> baseTrademarkList = manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    @ApiOperation("获取销售属性列表")
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    @ApiOperation("添加spu")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(
            @ApiParam(value = "添加的spu对象", required = true)
            @RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);

        return Result.ok();
    }

    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(
            @ApiParam("商品id")
            @PathVariable Long spuId) {
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    @ApiOperation("根据spuId获取销售属性")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(
            @ApiParam("商品id")
            @PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @ApiOperation("添加sku")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(
            @ApiParam("sku对象")
            @RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();

    }
    @ApiOperation("获取sku分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result list(
            @ApiParam(value = "第几页",required = true)
            @PathVariable Integer page,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Integer limit){
       IPage<SkuInfo> iPage = manageService.list(page,limit);
       return Result.ok(iPage);
    }

    @ApiOperation("上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(
            @ApiParam(value = "库存单元id",required = true)
            @PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    @ApiOperation("下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(
            @ApiParam(value = "库存单元id",required = true)
            @PathVariable Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }
}

